<?php
namespace app\index\model;
use think\Db;
use think\Model;

class UtilModel extends Model {

    /**
     * 导入用户账号
     * @return array
     * @throws PHPExcel_Exception
     * @throws PHPExcel_Reader_Exception
     */
    public function importHospital($file_path){
        //上传用户文件
        ini_set('display_errors' , 1);
        ini_set("memory_limit", "1024M");
        set_time_limit(0);
        import('util.PHPExcel.Classes.PHPExcle');
        import('util.PHPExcel.Classes.PHPExcel.IOFactory');

        $file_path = '.' . $file_path;
        $ext = strtolower(pathinfo($file_path, PATHINFO_EXTENSION));
        if ($ext == 'xlsx') {
            $objReader = \PHPExcel_IOFactory::createReader('Excel2007');
            $objPHPExcel = $objReader->load($file_path);
        } elseif ($ext == 'xls') {
            $objReader = \PHPExcel_IOFactory::createReader('Excel5');
            $objPHPExcel = $objReader->load($file_path);
        }
        else{
            return -1;
        }
        $sheet = $objPHPExcel->getSheet(0);
        $highestRow = $sheet->getHighestRow(); // 取得总行数
        $highestColumn = $sheet->getHighestColumn(); // 取得总列数
        set_time_limit(0);
        $hospitaArr = array();
        for($j=3;$j<=$highestRow;$j++) {
            $str = '';
            for ($k = 'A'; $k <= $highestColumn; $k++) {
                $str .= $objPHPExcel->getActiveSheet()->getCell("$k$j")->getValue() . '\\';//读取单元格
            }
            $strs = explode("\\", $str);
            //判断该数据

            $region['region_type']  = 2;
            $region['region_name']  = substr($strs[1],0,strlen($strs[1])-3);
            $region_id = Db::name('Region')->where($region)->value('region_id');
            $hospitaArr[] = array(
                'region_id'=>$region_id,
                'hospital_name'=>$strs[2]
            );
        }
        if($hospitaArr){
            return  Db::name('hospital')->insertAll($hospitaArr);
        }else{
            return -2;
        }
    }

    /**
     * 导入用户数据
     * @param $file_path
     */
    public function importUser($file_path){
        //上传用户文件
        ini_set('display_errors' , 1);
        ini_set("memory_limit", "1024M");
        set_time_limit(0);
        import('util.PHPExcel.Classes.PHPExcle');
        import('util.PHPExcel.Classes.PHPExcel.IOFactory');

        $file_path = '.' . $file_path;
        $ext = strtolower(pathinfo($file_path, PATHINFO_EXTENSION));
        if ($ext == 'xlsx') {
            $objReader = \PHPExcel_IOFactory::createReader('Excel2007');
            $objPHPExcel = $objReader->load($file_path);
        } elseif ($ext == 'xls') {
            $objReader = \PHPExcel_IOFactory::createReader('Excel5');
            $objPHPExcel = $objReader->load($file_path);
        }
        else{
            return -1;
        }
        $sheet = $objPHPExcel->getSheet(0);
        $highestRow = $sheet->getHighestRow(); // 取得总行数
        $highestColumn = $sheet->getHighestColumn(); // 取得总列数
        set_time_limit(0);
        $useraArr = array();
        for($j=2;$j<=$highestRow;$j++) {
            $str = '';
            for ($k = 'A'; $k <= $highestColumn; $k++) {
                $str .= $objPHPExcel->getActiveSheet()->getCell("$k$j")->getValue() . '\\';//读取单元格
            }
            $strs = explode("\\", $str);
            $useraArr[] = array(
                'user_name'=>trim($strs[0]),
                'user_email'=>trim($strs[1]),
                'user_tel'=>trim($strs[2]),
                'user_num'=>trim($strs[3]),
                'identity'=>trim($strs[4]),
                'area'=>trim($strs[5]),
                'profess'=>trim($strs[6]),
                'belong_brand'=>trim($strs[7]),
                'create_time'=>trim($strs[8]),
                'higher_lever'=>trim($strs[9]),
            );
        }
        if($useraArr){
            foreach ($useraArr as $user){
               $exist = Db::name('user')->where('user_name',$user['user_name'])->where('user_email',$user['user_email'])->find();
               if(!$exist){
                   Db::name('user')->insert($user);
               }
            }
            return 1;
//            return  Db::name('user')->insertAll($useraArr);
        }else{
            return -2;
        }
    }
}